package cate.game.framework.timer;

import easy.java.time.DateTool;
import lombok.Getter;
import lombok.Setter;

import java.util.StringJoiner;

/**
 * 时间任务
 */
@Getter
@Setter
public abstract class FIFOTimerTask implements RepeatedTimerTask<Long> {

    private long triggerTimeMs;
    private boolean done;

    public FIFOTimerTask() {
        this(System.currentTimeMillis());
    }

    public FIFOTimerTask(long triggerTimeMs) {
        this.triggerTimeMs = triggerTimeMs;
    }

    @Override
    public boolean isMatch(Long currentTimeMillis) {
        return currentTimeMillis >= triggerTimeMs;
    }

    @Override
    public void reset(Long aLong) {
        this.triggerTimeMs += aLong;
        this.setDone(false);
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", FIFOTimerTask.class.getSimpleName() + "[", "]")
                .add("triggerTimeMs=" + DateTool.instance().format(triggerTimeMs))
                .add("done=" + done)
                .toString();
    }
}
